package com.yingpai.fitness.dvp_2_0.main.activity.mine;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.yingpai.fitness.R;
import com.yingpai.fitness.base.BaseResponse;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.adapter.BaseAdapterHelper;
import com.yingpai.fitness.dvp_2_0.common.adapter.QuickAdapter;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.callback.Callback;
import com.yingpai.fitness.dvp_2_0.common.finalokgo.request.RequestType;
import com.yingpai.fitness.dvp_2_0.common.util.SharedPreferencesHelp;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Constant;
import com.yingpai.fitness.dvp_2_0.main.util.constant.Url;
import com.yingpai.fitness.entity.FollowAndFansBean;
import com.yingpai.fitness.entity.wx.TokenBean;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class MyFanOrFollowActivity extends CommonActivity implements OnRefreshLoadMoreListener {
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private int pageNum = 1;
    private int pageSize = 10;
    private String titleStr;
    private TokenBean tokenBean;
    private QuickAdapter<FollowAndFansBean.MapBean.CustomerListBean> quickAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.my_fan_or_follow_activity);
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            titleStr = bundle.getString("title");
        }
        setTitle(titleStr);

        tokenBean = (TokenBean) SharedPreferencesHelp.getBean(Constant.TOKENBEAN);
        refreshLayout.setOnRefreshLoadMoreListener(this);

        quickAdapter = new QuickAdapter<FollowAndFansBean.MapBean.CustomerListBean>(this, R.layout.my_fan_or_follow_list_item) {
            @Override
            protected void convert(BaseAdapterHelper helper, final FollowAndFansBean.MapBean.CustomerListBean item) {
                CircleImageView fan_or_follow_user_head_iv = helper.getView(R.id.fan_or_follow_user_head_iv);//用户头像
                Glide.with(context)
                        .load(item.getPhotoUrl())
                        .placeholder(R.mipmap.icon_user_default)
                        .into(fan_or_follow_user_head_iv);

                helper.setText(R.id.fan_or_follow_user_name, item.getNickName());

                final TextView fan_or_follow_btn = helper.getView(R.id.fan_or_follow_btn);//关注
                if (item.isFollowed()) {
                    fan_or_follow_btn.setText("已关注");
                } else {
                    fan_or_follow_btn.setText("关注");
                }

                fan_or_follow_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        HttpParams params = new HttpParams();
                        params.put("myId", tokenBean.getId());
                        params.put("otherId", item.getId());
                        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + Url.FOLLOW_OR_UNFOLLOW, params, Url.FOLLOW_OR_UNFOLLOW, new Callback<BaseResponse>() {
                            @Override
                            public void onSuccess(BaseResponse baseResponse) {
                                if (item.isFollowed()) {
                                    item.setFollowed(false);
                                } else {
                                    item.setFollowed(true);
                                }
                                notifyDataSetChanged();
                            }
                        });
                    }
                });
            }
        };

        listview.setAdapter(quickAdapter);
    }

    @Override
    protected void initData() {
        pageNum = 1;
        if ("我的关注".equals(titleStr)) {
            requestData(Url.GET_MY_FOLLOWER);
        } else if ("我的粉丝".equals(titleStr)) {
            requestData(Url.GET_MY_FANS);
        }
    }

    private void requestData(String url) {
        showDialog(this, null);
        HttpParams httpParams = new HttpParams();
        httpParams.put("myId", tokenBean.getId());
        httpParams.put("customerId", tokenBean.getId());
        httpParams.put("pageNum", pageNum++);
        httpParams.put("pageSize", pageSize);
        finalOkGo.request(RequestType.POST, Contacts.HOST_SERVER + url, httpParams, url, new Callback<FollowAndFansBean>() {
            @Override
            public void onSuccess(FollowAndFansBean followAndFansBean) {
                if (followAndFansBean.getResult().equals(Constant.REQUEST_SUCCESS) && followAndFansBean.getMap() != null) {
                    refreshLayout.setNoMoreData(false);
                    if (pageNum - 1 == 1) {
                        if(followAndFansBean.getMap().getCustomerList()!=null&&followAndFansBean.getMap().getCustomerList().size()!=0){
                            empty.setVisibility(View.GONE);
                            quickAdapter.clear();
                            quickAdapter.addAll(followAndFansBean.getMap().getCustomerList());
                        }else{
                            empty.setVisibility(View.VISIBLE);
                        }
                    } else {
                        quickAdapter.addAll(followAndFansBean.getMap().getCustomerList());
                    }
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadMore();
                    quickAdapter.notifyDataSetChanged();
                } else {
                    if (pageNum - 1 == 1) {
                        empty.setVisibility(View.VISIBLE);
                    } else {
                        empty.setVisibility(View.GONE);
                    }
                    refreshLayout.finishLoadMoreWithNoMoreData();
                    showToast(followAndFansBean.getMsg());
                }
                dismissDialog();
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                if (pageNum - 1 == 1) {
                    empty.setVisibility(View.VISIBLE);
                } else {
                    empty.setVisibility(View.GONE);
                }
                dismissDialog();
            }
        });
    }

    @Override
    @OnClick({})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {

        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if ("我的关注".equals(titleStr)) {
            requestData(Url.GET_MY_FOLLOWER);
        } else if ("我的粉丝".equals(titleStr)) {
            requestData(Url.GET_MY_FANS);
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        if ("我的关注".equals(titleStr)) {
            requestData(Url.GET_MY_FOLLOWER);
        } else if ("我的粉丝".equals(titleStr)) {
            requestData(Url.GET_MY_FANS);
        }
    }
}
